package org.itboys.mobile.service.pos;


import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.json.JsonUtils;
import org.itboys.mobile.entity.pos.store.PosStoreAndPrice;
import org.itboys.mobile.entity.pos.store.PosStoreAndPriceResult;
import org.itboys.mobile.service.pos.util.PosHttpHelper;
import org.itboys.mobile.service.pos.util.PosMD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 作者：jiangxiong
 * 日期：2016年7月8日
 * 联系方式 ：empty90911@126.com
 * 描述：花果山_pos_商品（按门店同步商品价格和库存）_
 */
@Service
public class PosStoreAndPriceService {
	private final static Logger logger = LoggerFactory.getLogger(PosHttpHelper.class);


	/**
	 * 获取（门店）仓库商品的库存和价格
	 * @param BranchCode
	 * @return
	 */
	public static  List<PosStoreAndPrice>  getStoreAndPriceByShop(String BranchCode){
		
		Map<String,Object> param =new HashMap<String, Object>();
		param.put("Method", "GetBranchItemAll");
		//时间搓
		Long timeStamp=new Date().getTime();
		param.put("TimeStamp", timeStamp);
		Integer Page =1;
		//按照门店，获取门店所有商品的价格和库存
		List<PosStoreAndPrice> posStoreAndPrices = new ArrayList<PosStoreAndPrice>();
		
		while(true){
			String data = "{\"Page\":" + Page + ",\"BranchCode\":\"" + BranchCode +"\"}";
			//签名
			String Signature = PosMD5Util.MD5(PosCommons.token+timeStamp+data);
			param.put("data", data);
			param.put("Signature", Signature.toUpperCase());

			String responsResult= PosHttpHelper.doPost(PosCommons.url, param);

			PosStoreAndPriceResult result=  JsonUtils.jsonToObject(PosStoreAndPriceResult.class, responsResult);
	    	Integer code = result.ResCode;
		    if(code!=0){
		    	throw new ErrorException(result.Msg);
		    }else{
		    	if(result.ResData.size()==0){
		    		break;
		    	}
		    	for(PosStoreAndPrice storeAndPrice:result.ResData){
		    		posStoreAndPrices.add(storeAndPrice);
		    	}
		    	Page++;
		    }
		}
		return posStoreAndPrices;

	}
	/**
	 * 按门店获取单个商品价格和库存。仅作测试
	 * @param BranchCode
	 * @return
	 */
	public static  void getStoreAndPriceSingleByShop(String ItemCode,String BranchCode){

		Map<String,Object> param =new HashMap<String, Object>();
		param.put("Method", "GetBranchItem");
		//时间搓
		Long timeStamp=new Date().getTime();
		param.put("TimeStamp", timeStamp);
		Integer Page =1;
		//按照门店，获取门店所有商品的价格和库存
		List<PosStoreAndPriceResult> posStoreAndPrices = new ArrayList<PosStoreAndPriceResult>();


			String data = "{\"ItemCode\":" + ItemCode + ",\"BranchCode\":\"" + BranchCode +"\"}";
			//签名
			String Signature =PosMD5Util.MD5(PosCommons.token+timeStamp+data);
			param.put("data", data);
			param.put("Signature", Signature.toUpperCase());
			String responsResult= PosHttpHelper.doPost(PosCommons.url, param);
			System.out.println("------"+responsResult);
	}
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		getStoreAndPriceSingleByShop("6902538005141", "00010001");
		int i=0;
		/*for(PosStoreAndPrice storeAndPrice:getStoreAndPriceByShop("00010001")){
			logger.info("门店编码:"+storeAndPrice.BranchCode+"--条码barcode="+storeAndPrice.Barcode+"--编码itemCode="+storeAndPrice.ItemCode+"--价格price="+storeAndPrice.SalePrice+""+"--库存="+storeAndPrice.StockQty+"");
		i++;
		}*/
		System.out.println("门店-KF01001获取库存数据："+i+"条");
	}


  }
